Skip to content

Conversation

@swote-git
Copy link
Contributor

@swote-git swote-git commented Aug 27, 2025

Add test coverage to ensure comma operators remain properly rejected in #if directives.

Per CWG 1436, comma is not among the permitted operators in preprocessor conditional expressions. This test prevents regressions and clarifies the intended behavior.

Fixes #132822

…expressions

Add test coverage for comma operator usage in #if preprocessor directives
to ensure it continues to be properly rejected across all C++ standard versions.
Per CWG 1436, comma operators are not among the permitted operators in
preprocessor conditional expressions.

Addresses llvm#132822
@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Aug 27, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 27, 2025

@llvm/pr-subscribers-clang

Author: NohHyeon Kwon (swote-git)

Changes

Add test coverage to ensure comma operators remain properly rejected in #if directives.

Per CWG 1436, comma is not among the permitted operators in preprocessor conditional expressions. This test prevents regressions and clarifies the intended behavior.

Addresses #132822


Full diff: https://github.com/llvm/llvm-project/pull/155570.diff

1 Files Affected:

  • (added) clang/test/Preprocessor/cxx_oper_comma.cpp (+26)
diff --git a/clang/test/Preprocessor/cxx_oper_comma.cpp b/clang/test/Preprocessor/cxx_oper_comma.cpp
new file mode 100644
index 0000000000000..5589024ede01c
--- /dev/null
+++ b/clang/test/Preprocessor/cxx_oper_comma.cpp
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++98
+// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++11
+// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++14
+// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++17
+// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++20
+// RUN: %clang_cc1 -E -pedantic-errors %s -verify -std=c++23
+
+// Test 1: Top-level comma
+// expected-error@+1 {{expected end of line in preprocessor expression}}
+#if 1, 2
+#endif
+
+// Test 2: Comma in conditional expression
+// expected-error@+1 {{comma operator in operand of #if}}
+#if 1 ? 1, 0 : 3
+#endif
+
+// Test 3: Parenthesized comma
+// expected-error@+1 {{comma operator in operand of #if}}
+#if (1, 2)
+#endif
+
+// Test 4: Multiple commas
+// expected-error@+1 {{expected end of line in preprocessor expression}}
+#if 1, 2, 3
+#endif

@swote-git
Copy link
Contributor Author

Currently, this only about adding test code.

While adding these tests, I noticed two potential improvements to the current error message "comma operator in operand of #if":

  1. The same message appears for #elif, #ifdef, etc.
  2. Per CWG 1436, comma operators are explicitly not permitted in preprocessor conditionals. The current message describes where the comma is, but doesn't clearly convey why it's prohibited.

A clearer message should be more clear.

Copy link
Member

@Sirraide Sirraide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, though I wonder if it should be part of the DR tests instead.

CC @Endilll

@Sirraide Sirraide requested a review from Endilll September 22, 2025 06:35
@swote-git
Copy link
Contributor Author

@Sirraide Thanks for your review!

Should I move this to clang/test/CXX/drs/cwg1436.cpp and format it according to the DR test conventions?

@Sirraide
Copy link
Member

Should I move this to clang/test/CXX/drs/cwg1436.cpp and format it according to the DR test conventions?

Something like that yeah; I think there might already be a file that contains some of the 14XX tests somewhere but I’m not sure; I’ve tagged Vlad about this who should be able to tell you where exactly to put it.

@cor3ntin
Copy link
Contributor

@shafik was https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#3017 resolved ?

Should I move this to clang/test/CXX/drs/cwg1436.cpp and format it according to the DR test conventions?

Something like that yeah; I think there might already be a file that contains some of the 14XX tests somewhere but I’m not sure; I’ve tagged Vlad about this who should be able to tell you where exactly to put it.

Yes, please!

@cor3ntin cor3ntin added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Sep 22, 2025
@DavidSpickett
Copy link
Collaborator

DavidSpickett commented Sep 22, 2025

There are some tests in clang/test/Preprocessor/expr_invalid_tok.c right now. Found with a regex find in files #if\s+.*,.

@swote-git
Copy link
Contributor Author

@DavidSpickett Thanks for your help.

I can see similar test in expr_invalid_tok.c

// PR2284 - The constant-expr production does not including comma.
// CHECK: [[@LINE+1]]:14: error: expected end of line in preprocessor expression
#if 1 ? 2 : 0, 1
#endif

What I wanted to write a little more comprehensively was because of the contents of expr_comma.c.

// Comma is not allowed in C89
// RUN: not %clang_cc1 -E %s -std=c89 -pedantic-errors

// Comma is allowed if unevaluated in C99
// RUN: %clang_cc1 -E %s -std=c99 -pedantic-errors 

// PR2279

#if 0? 1,2:3
#endif

@Endilll
Copy link
Contributor

Endilll commented Sep 22, 2025

I'd love if this PR added tests for Core issues 1436 and 3017, but I don't see which wording we should test against. The only proposed resolution from 2012-02 by Richard is obviously incomplete.

I think we shouldn't touch DR tests until more information from Core arrives.

@swote-git
Copy link
Contributor Author

@Endilll Thanks for pointing out CWG 3017.
"Test 2" is testing exactly the same case as the CWG 3017 example:

// Test 2: Comma in conditional expression
// expected-error@+1 {{comma operator in operand of #if}}
#if 1 ? 1, 0 : 3
#endif

Should I add a comment referencing CWG 3017 to make this connection explicit?

@Endilll
Copy link
Contributor

Endilll commented Sep 22, 2025

Sure, additional context never hurts.

- Add test coverage for comma operator in #elif directive
- Reference CWG 3017 in comments for additional context
- Addresses reviewer feedback
@swote-git
Copy link
Contributor Author

@DavidSpickett @Endilll i made changes for adding context(cwg3017) and adding tests(#elif , #if ,).

Please review when you have time.

@DavidSpickett
Copy link
Collaborator

Tests look fine to me now, but deferring to @Endilll as the domain expert here.

Copy link
Contributor

@Endilll Endilll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your work

@shafik
Copy link
Collaborator

shafik commented Sep 22, 2025

@shafik was https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#3017 resolved ?

No, it is still active. It looks like CWG1436 is where the action will be moving forward.

Remove defined(__has_embed) check and use since-cxx23 verify prefix to align with C++ DR test conventions.
Copy link
Contributor

@Endilll Endilll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good to go after you add #if , test suggested earlier

@swote-git
Copy link
Contributor Author

swote-git commented Sep 24, 2025

I think this is good to go after you add #if , test suggested earlier

Regarding the #if , test case, I believe I've already added it in the previous version of the patch(test 6). You can find it right here on line36-39.

Thanks for your support!

@AaronBallman AaronBallman merged commit 0149864 into llvm:main Oct 23, 2025
9 checks passed
@github-actions
Copy link

@swote-git Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

dvbuka pushed a commit to dvbuka/llvm-project that referenced this pull request Oct 27, 2025
…onditionals (llvm#155570)

Add test coverage to ensure comma operators remain properly rejected in
`#if` directives.

Per CWG 1436, comma is not among the permitted operators in preprocessor
conditional expressions. This test prevents regressions and clarifies
the intended behavior.

Fixes llvm#132822
Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Oct 29, 2025
…onditionals (llvm#155570)

Add test coverage to ensure comma operators remain properly rejected in
`#if` directives.

Per CWG 1436, comma is not among the permitted operators in preprocessor
conditional expressions. This test prevents regressions and clarifies
the intended behavior.

Fixes llvm#132822
aokblast pushed a commit to aokblast/llvm-project that referenced this pull request Oct 30, 2025
…onditionals (llvm#155570)

Add test coverage to ensure comma operators remain properly rejected in
`#if` directives.

Per CWG 1436, comma is not among the permitted operators in preprocessor
conditional expressions. This test prevents regressions and clarifies
the intended behavior.

Fixes llvm#132822
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Comma expressions rejected in #if for C++23

8 participants